package com.enjoyor.soa.traffic.server.efoms.dubbo.manage;

import com.alibaba.dubbo.config.annotation.Service;
import com.enjoyor.soa.traffic.api.dto.efoms.EvalItemInfoDto;
import com.enjoyor.soa.traffic.core.efoms.domain.EvalItemInfo;
import com.enjoyor.soa.traffic.core.efoms.service.IEvalItemInfoService;
import com.enjoyor.soa.traffic.frame.support.util.DomainUtil;
import com.enjoyor.soa.traffic.util.enums.EnumAppCode;
import com.enjoyor.soa.traffic.util.helper.LoggerHelper;
import com.enjoyor.soa.traffic.util.helper.ReflectHelper;
import com.enjoyor.soa.traffic.util.helper.ResultHelper;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class ManageEvalItemInfoService {

    @Autowired
    private IEvalItemInfoService evalItemInfoService;

    /**
     * 新增评价项
     *
     * @param evalItemInfoDto
     * @param username
     */
    public ResultPojo insertEvalItemInfo(EvalItemInfoDto evalItemInfoDto, String username) {
        try {
            if (evalItemInfoDto == null || evalItemInfoDto.getEvalItemExplain() == null || evalItemInfoDto.getEvalItemName() == null)
                return ResultHelper.getResult(EnumAppCode.PARAM_ERR);
            EvalItemInfo evalItemInfo = ReflectHelper.beanToBean(evalItemInfoDto, EvalItemInfo.class);
            evalItemInfoService.insertEvalItemInfo(evalItemInfo, username);
            return ResultHelper.getResult(EnumAppCode.SUCCESS);
        } catch (Exception e) {
            LoggerHelper.LOG.error(evalItemInfoDto, e);
            return ResultHelper.getResult(e, EnumAppCode.OPERATOR_FAILURE);
        }

    }

    /**
     * 修改评价项
     *
     * @param evalItemInfoDto
     * @param username
     */
    public ResultPojo updateEvalItemInfo(EvalItemInfoDto evalItemInfoDto, String username) {
        try {
            if (evalItemInfoDto == null || evalItemInfoDto.getEvalItemExplain() == null || evalItemInfoDto.getEvalItemName() == null)
                if (evalItemInfoDto.getEvalItemId() == null || evalItemInfoDto.getEvalItemId().equals(""))
                    return ResultHelper.getResult(EnumAppCode.PARAM_ERR);
            EvalItemInfo evalItemInfo = ReflectHelper.beanToBean(evalItemInfoDto, EvalItemInfo.class);
            evalItemInfoService.updateEvalItemInfo(evalItemInfo, username);
            return ResultHelper.getResult(EnumAppCode.SUCCESS);
        } catch (Exception e) {
            LoggerHelper.LOG.error(evalItemInfoDto, e);
            return ResultHelper.getResult(e, EnumAppCode.OPERATOR_FAILURE);
        }

    }

    /**
     * 删除评价项
     *
     * @param evalItemId
     * @param username
     */
    public ResultPojo deleteEvalItemInfo(String evalItemId, String username) {
        try {
            if (evalItemId == null || evalItemId.equals(""))
                return ResultHelper.getResult(EnumAppCode.PARAM_ERR);
            evalItemInfoService.deleteEvalItemInfo(evalItemId, username);
            return ResultHelper.getResult(EnumAppCode.SUCCESS);
        } catch (Exception e) {
            LoggerHelper.LOG.error(evalItemId, e);
            return ResultHelper.getResult(e, EnumAppCode.OPERATOR_FAILURE);
        }
    }

    /**
     * 查询评价项
     *
     * @param evalItemId
     * @return
     */
    public ResultPojo getEvalItemList(String evalItemId) {
        try {
            List<EvalItemInfo> evalItemInfoList = evalItemInfoService.getEvalItemList(evalItemId);
            List<EvalItemInfoDto> evalItemInfoDtoList = null;
            if (evalItemInfoList != null && evalItemInfoList.size() > 0)
                evalItemInfoDtoList = DomainUtil.merge2ListJson(evalItemInfoList, EvalItemInfoDto.class);
            return ResultHelper.getResult(evalItemInfoDtoList);
        } catch (Exception e) {
            LoggerHelper.LOG.error(evalItemId, e);
            return ResultHelper.getResult(e, EnumAppCode.OPERATOR_FAILURE);
        }
    }
}
